/*
 * 
 */
package server;
	
import java.awt.EventQueue;
	
	import javax.swing.JCheckBox;
	import javax.swing.JFrame;
	import javax.swing.JPanel;
	import javax.swing.JScrollPane;
	import javax.swing.JLabel;
	import javax.swing.JTextField;
	import javax.swing.JComboBox;
	import javax.swing.ScrollPaneConstants;
	import javax.swing.JSeparator;
	import javax.swing.SwingConstants;
	import java.awt.SystemColor;
	import javax.swing.JTextArea;
	
	import abstractions.AtomId;
	import abstractions.*;


	import javax.swing.JButton;

	import java.awt.event.ActionListener;
	import java.awt.event.ActionEvent;
	import java.io.File;
	import java.io.FileInputStream;
	import java.io.FileOutputStream;
	import java.io.ObjectInputStream;
	import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
	import java.rmi.server.UnicastRemoteObject;
	
import static server.Server.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;
/**
 * @author rdru
 * INV: 
 */
@SuppressWarnings("rawtypes")
public class ServerStub extends JFrame {
	//private static final long serialVersionUID = 1L;
	public JFrame monFrame;
	private JPanel contentPane;
	private JTextField txtServerId;
	private JTextField txtdocId;
	private JTextField txtAtomId;
	private JTextField txtUserId;
	private static JTextArea serverLog;
	
	static StringBuilder fullLog = new StringBuilder();
	private JTextField txtServerFreq;
	private JTextField txtclientId;
	
    public static void serverLogAdd(boolean addToMonitor, String s) {
		fullLog.append(s);
		if (addToMonitor) {
			serverLog.append(s);
	        serverLog.setCaretPosition(serverLog.getDocument().getLength());
		}
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerStub frame = new ServerStub();
					frame.setVisible(true);    
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	private ServerServices server;
	private ServerServicesStub impServerServices;

	public ServerStub() {
		
		
		monFrame = this;
		setTitle("Server Stub");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(5, 5, 650, 619);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel sharedDocuments = new JLabel("ServerId");
		sharedDocuments.setBounds(19, 38, 80, 16);
		contentPane.add(sharedDocuments);
		
		txtServerId = new JTextField();
		txtServerId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtServerId.setText("1");
		txtServerId.setBounds(142, 32, 59, 21);
		contentPane.add(txtServerId);
		txtServerId.setColumns(10);
		
		JLabel lblOpenDocuments = new JLabel("docId");
		lblOpenDocuments.setBounds(19, 65, 115, 16);
		contentPane.add(lblOpenDocuments);
		
		txtdocId = new JTextField();
		txtdocId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtdocId.setText("1");
		txtdocId.setBounds(142, 63, 59, 21);
		contentPane.add(txtdocId);
		txtdocId.setColumns(10);
		
		JLabel lblRegisteredUsers = new JLabel("AtomId");
		lblRegisteredUsers.setBounds(19, 101, 122, 16);
		contentPane.add(lblRegisteredUsers);
		
		txtAtomId = new JTextField();
		txtAtomId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtAtomId.setText("1");
		txtAtomId.setBounds(142, 95, 59, 21);
		contentPane.add(txtAtomId);
		txtAtomId.setColumns(10);
		
		JLabel lblActiveUsers = new JLabel("UserId");
		lblActiveUsers.setBounds(19, 167, 109, 16);
		contentPane.add(lblActiveUsers);
		
		txtUserId = new JTextField();
		txtUserId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtUserId.setText("1");
		txtUserId.setBounds(142, 161, 59, 21);
		contentPane.add(txtUserId);
		txtUserId.setColumns(10);
		
		JLabel lblServerStatusLog = new JLabel("Server Status Log");
		lblServerStatusLog.setBounds(10, 278, 128, 16);
		contentPane.add(lblServerStatusLog);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(10, 305, 610, 267);
		contentPane.add(scrollPane_2);
		
		serverLog = new JTextArea();
		serverLog.setLineWrap(true);
		scrollPane_2.setViewportView(serverLog);
		
		JSeparator separator = new JSeparator();
		separator.setForeground(SystemColor.windowBorder);
		separator.setOrientation(SwingConstants.VERTICAL);
		separator.setBounds(319, 11, 12, 235);
		contentPane.add(separator);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setForeground(SystemColor.windowBorder);
		separator_2.setBounds(10, 257, 615, 10);
		contentPane.add(separator_2);
		
		JButton btnNewButton = new JButton("lockAtom");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ClientServices cs = impServerServices.clientList.get(Integer.parseInt(txtclientId.getText()));
				try {
					cs.lockAtom(1, Integer.parseInt(txtdocId.getText()), new AtomId(new ArrayList<Integer>(Integer.parseInt(txtAtomId.getText()))), Integer.parseInt(txtUserId.getText()));
				} catch (NumberFormatException e) {
					System.out.println("number");
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				serverLog.append("LockAtom requisitado\n");
			}
		});
		btnNewButton.setBounds(414, 35, 145, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("unlockAtom");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				ClientServices cs = impServerServices.clientList.get(Integer.parseInt(txtclientId.getText()));
				try {
					cs.unlockAtom(1, 1 , new AtomId(new ArrayList<Integer>(Integer.parseInt(txtAtomId.getText()))));
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				serverLog.append("unLockAtom requisitado\n");
			}
		});
		btnNewButton_1.setBounds(414, 72, 145, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("AddClientToDoc");
		btnNewButton_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ClientServices cs = impServerServices.clientList.get(Integer.parseInt(txtclientId.getText()));
				try {
					cs.addClientToDoc(1, Integer.parseInt(txtdocId.getText()), Integer.parseInt(txtUserId.getText()));
				} catch (NumberFormatException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (RemoteException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				serverLog.append("AddClientToDoc requisitado\n");
			}			
		});
		btnNewButton_2.setBounds(414, 106, 145, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("RemoveClientFromDoc");
		btnNewButton_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ClientServices cs = impServerServices.clientList.get(Integer.parseInt(txtclientId.getText()));
				try {
					cs.removeClientFromDoc(1, Integer.parseInt(txtdocId.getText()), Integer.parseInt(txtUserId.getText()));
				} catch (NumberFormatException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (RemoteException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				serverLog.append("RemoveClientToDoc requisitado\n");				
			}
		});
		btnNewButton_3.setBounds(414, 140, 145, 23);
		contentPane.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("UpdateDoc");
		btnNewButton_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				LinkedList<CommandC> lista1 = new LinkedList<CommandC>();
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(1)),0) , new Part("") , new Part("")));
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(2)),0) , new Part("") , new Part("")));
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(3)),0) , new Part("") , new Part("")));
				// trocando os dois primeiros versos de lugar
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(1)),0) , new Part("Minhas duvidas sao incertas") , new Part("Minhas certezas duvidosas")));
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(1)),0) , new Part("Minhas certezas duvidosas") , new Part("Minhas duvidas sao incertas")));
				ClientServices cs = impServerServices.clientList.get(Integer.parseInt(txtclientId.getText()));
				try {
					cs.updateDoc(Integer.parseInt(txtclientId.getText()), Integer.parseInt(txtdocId.getText()), lista1);
				} catch (NumberFormatException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				} catch (RemoteException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				}
				serverLog.append("UpdateDoc requisitado\n");
			}
			
		});
		btnNewButton_4.setBounds(414, 174, 145, 23);
		contentPane.add(btnNewButton_4);
		
		JButton btnNewButton_8 = new JButton("updateServerFreq");
		btnNewButton_8.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ClientServices cs = impServerServices.clientList.get(Integer.parseInt(txtclientId.getText()));
				try {
					cs.updateServerFreq(Integer.parseInt(txtServerId.getText()), Integer.parseInt(txtServerFreq.getText()));
				} catch (NumberFormatException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (RemoteException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				serverLog.append("updateServerFreq requisitado\n");				
			}
		});
		btnNewButton_8.setBounds(414, 211, 145, 23);
		contentPane.add(btnNewButton_8);
		
		JLabel lblLogname = new JLabel("serverFreq");
		lblLogname.setBounds(19, 203, 109, 16);
		contentPane.add(lblLogname);
		
		txtServerFreq = new JTextField();
		txtServerFreq.setHorizontalAlignment(SwingConstants.RIGHT);
		txtServerFreq.setText("30");
		txtServerFreq.setColumns(10);
		txtServerFreq.setBounds(142, 194, 59, 21);
		contentPane.add(txtServerFreq);
		
		JLabel lblClientid = new JLabel("clientId");
		lblClientid.setBounds(19, 134, 109, 16);
		contentPane.add(lblClientid);
		
		txtclientId = new JTextField();
		txtclientId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtclientId.setText("1");
		txtclientId.setColumns(10);
		txtclientId.setBounds(142, 128, 59, 21);
		contentPane.add(txtclientId);
		
		try {
			//make rmi		
			rmiServer aux  = new rmiServer();
			impServerServices = new ServerServicesStub(serverLog);			
			server = aux.rmiServerHandler(ServerServicesStub.class, impServerServices, 8888);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
